package com.ftar.urgent.viewmodel;

import static com.ftar.urgent.api.KnowledgeApi.getIndustryTree;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.ftar.urgent.api.resp.GetIndustryTreeResp;
import com.ftar.urgent.databinding.ActivityCatalogFirstBinding;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.collection.ListUtil;


public class FirstActivityModel extends BaseModel {

    private String TAG = this.getClass().getSimpleName();

    public MutableLiveData<List<GetIndustryTreeResp>> mRootList = new MutableLiveData<>(new ArrayList<>());

    private Context mContext;
    private ActivityCatalogFirstBinding mBinding;
    private List<List<GetIndustryTreeResp>> pageList = new ArrayList<>();

    private int currentPage = 1;
    private int totalSize = 0;
    private int pageSize = 6;
    private int totalPage = 1;
    private String  currentPageStr;
    private String  totalPageStr;

    public FirstActivityModel(Context context, ActivityCatalogFirstBinding binding) {
        mContext = context;
        mBinding = binding;
    }


    public Context getmContext() {
        return mContext;
    }

    public ActivityCatalogFirstBinding getmBinding() {
        return mBinding;
    }

    public List<List<GetIndustryTreeResp>> getPageList() {
        return pageList;
    }

    public List<GetIndustryTreeResp> getmRootList() {
        return mRootList.getValue();
    }

    public int getTotalSize() {
        return totalSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public void setTotalSize(int totalSize) {
        this.totalSize = totalSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    /**
     * 初始化分类树
     */
    public void initIndustryTree() {
        FirstActivityModel model = this;
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<GetIndustryTreeResp> datas = new ArrayList<>();
                try {
                    datas = getIndustryTree();
                } catch (IOException e) {
                    Log.e(TAG,"getIndustryTree error!!!");
                }
                totalSize = datas.size();
                totalPage = (totalSize + pageSize - 1) / pageSize;
                model.setTotalPageStr();
                model.setCurrentPageStr();
                pageList = ListUtil.partition(datas, pageSize);
                mRootList.postValue(pageList.get(getCurrentPage() - 1));
                mBinding.setFirstModel(model);
            }
        }).start();
    }

    public void setCurrentPageStr() {
        this.currentPageStr = String.format("第%s页", this.currentPage);
    }

    public void setTotalPageStr() {
        this.totalPageStr = String.format("共%s页", this.totalPage);
    }
    public String getCurrentPageStr() {
        return currentPageStr;
    }

    public String getTotalPageStr() {
        return totalPageStr;
    }

    public void goPreviousPage() {
        if (this.currentPage == 1) {
            return;
        }
        this.currentPage--;
        setCurrentPageStr();
        mRootList.postValue(pageList.get(currentPage - 1));
        this.mBinding.setFirstModel(this);
    }
    public void goNextPage() {
        if (this.currentPage == this.totalPage) {
            return;
        }
        this.currentPage++;
        setCurrentPageStr();
        mRootList.postValue(this.pageList.get(this.currentPage - 1));
        this.mBinding.setFirstModel(this);
    }

}
